"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具显示卷组和磁盘使用信息
Create At   : 2023/4/11
Owner       : opentestcase019
Description :
    1.显示卷组和磁盘使用信息，不指定-m,-t,-U参数
    2.显示卷组和磁盘使用信息，指定-m measure_type参数为正常值
    3.显示卷组和磁盘使用信息，指定-m measure_type参数为非法值
    4.显示卷组和磁盘使用信息，指定-t show_type参数为正常值
    5.显示卷组和磁盘使用信息，指定-t show_type参数为非法值
    6.显示卷组和磁盘使用信息，指定-U UDS:socket_domain参数为正常值
    7.显示卷组和磁盘使用信息，指定指定错误的-U UDS:socket_domain参数和权限不足的socket_domain
    8.显示卷组和磁盘使用信息，指定-m,-t,-U参数的不同组合
    9.在集群停止的情况下执行
Expect      :
    1.显示成功
    2.显示成功
    3.合理报错measure_type error.
    4.显示成功
    5.合理报错The show type should be [d|t].
    6.显示成功
    7.合理报错Failed to get uds connection.
    8.显示成功
    9.合理报错
History     :
"""
import os
import re
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()

    def test_sharestorage_tool(self):
        step1 = "-----step1:显示卷组和磁盘使用信息，不指定-m,-t,-U参数;expect:显示成功-----"
        self.logger.info(step1)
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lsvg"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        exp = f"vg_name.*volume_count.*size.*free.*used.*percent\\(\\%\\)"
        search_res = re.search(exp, execute_msg, re.S)
        self.assertIsNotNone(search_res, '执行失败' + step1)
        self.assertIn("Succeed to display lsvg info.",
                      execute_msg, "执行失败" + step1)

        step2 = "-----step2:显示卷组和磁盘使用信息，指定-m measure_type参数" \
                "为正常值;expect:显示成功-----"
        self.logger.info(step2)
        cmd_list = ['-m B', '-m K', '-m M', '-m G', '-m T']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lsvg {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            self.assertIn("Succeed to display lsvg info.",
                          execute_msg, "执行失败" + step2)

        step3 = "-----step3:显示卷组和磁盘使用信息，指定-m measure_type参数" \
                "为非法值;expect:合理报错measure_type error.-----"
        self.logger.info(step3)
        cmd_list = ['-m A', '-m b', '-m g']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lsvg {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            self.assertIn("measure_type error.", execute_msg,
                          "执行失败" + step3)

        step4 = "-----step4:显示卷组和磁盘使用信息，指定-t show_type参数" \
                "为正常值;expect:显示成功-----"
        self.logger.info(step4)
        cmd_list = ['-t d', '-t t']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lsvg {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            self.assertIn("Succeed to display lsvg info.",
                          execute_msg, "执行失败" + step4)

        step5 = "-----step5:显示卷组和磁盘使用信息，指定-t show_type参数为非法值;" \
                "expect:合理报错The show type should be [d|t].-----"
        self.logger.info(step5)
        cmd_list = ['-t n', '-t D', '-t T']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lsvg {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            self.assertIn("The show type should be [d|t].",
                          execute_msg, "执行失败" + step5)

        step6 = "-----step6:显示卷组和磁盘使用信息，指定-U UDS:socket_domain" \
                "参数为正常值;expect:显示成功-----"
        self.logger.info(step6)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd lsvg -U {macro.UDS_PATH}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("Succeed to display lsvg info.",
                      execute_msg, "执行失败" + step6)

        step7 = "-----step7:显示卷组和磁盘使用信息，指定指定错误的" \
                "-U UDS:socket_domain参数和权限不足的socket_domain;" \
                "expect:合理报错Failed to get uds connection.-----"
        self.logger.info(step7)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd lsvg -U {macro.UDS_PATH}.cfg"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(self.constant.failed_to_get_uds_connection,
                      execute_msg, "执行失败" + step6)
        execute_cmd = f"chmod 000 {macro.UDS_PATH[4::]};" \
                      f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd lsvg -U {macro.UDS_PATH};" \
                      f"chmod 600 {macro.UDS_PATH[4::]};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(self.constant.failed_to_get_uds_connection,
                      execute_msg, "执行失败" + step7)

        step8 = "-----step8:显示卷组和磁盘使用信息，指定-m,-t,-U参数的不同组合;" \
                "expect:显示成功-----"
        self.logger.info(step8)
        cmd_list = ['-m B -t d', '-m G -t t',
                    f'-m M -U {macro.UDS_PATH}',
                    f'-t t -U {macro.UDS_PATH}']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lsvg {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            self.assertIn("Succeed to display lsvg info.",
                          execute_msg, "执行失败" + step8)

        step9 = "-----step9:在集群停止的情况下执行;expect:合理报错-----"
        self.logger.info(step9)
        stop_msg = self.primary_sh.exec_cm_ctl(mode='stop')
        self.logger.info(stop_msg)
        self.assertIn(self.constant.cm_stop_success_msg,
                      stop_msg, '执行失败' + step9)
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lsvg"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(self.constant.failed_to_get_uds_connection,
                      execute_msg, "执行失败" + step9)

    def tearDown(self):
        self.logger.info("-----恢复环境-----")
        start_msg = self.primary_sh.exec_cm_ctl(mode='start')
        self.logger.info(start_msg)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn(self.constant.cm_start_success_msg,
                      start_msg, '启动集群失败')
        self.assertIn('cluster_state   : Normal', status, '执行失败')
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
